﻿Imports DataObject
Imports BusinessObject
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data.SqlClient

Public Class frmHopDongLDDetail

    Private ptyHopDongLD As New HopDongLD
    Private oHopDongLD As New BusHopDongLD
    Private oFunction As New hDCommonFunction
    Private ds As New dsQLHD
    Private rowTLDK As DataRow
    Private tableTLDK As New DataTable
    Private taiLieuDK(50) As Integer
    Private index As Integer = 0

    Public Property TableTaiLieuDinhKem() As DataTable
        Get
            Return tableTLDK
        End Get
        Set(ByVal value As DataTable)
            tableTLDK = value
        End Set
    End Property

    Public Sub GetValue(ByVal value As DataTable)
        grdList.DataSource = value
    End Sub

    Private Sub frmHopDongLDDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Me.eActionMode = EditMode.AddNew) Then
            Me.Text = "Thêm mới hợp đồng lao động"
        Else
            Me.Text = "Sửa hợp đồng lao động"
        End If
        txtMaHopDongLD.Focus()
        LoadAllCombo()
        LoadDataGrid()
        DataBindingToControl(GroupBox1)
    End Sub

    Public Overrides Function ValidateData() As Boolean
        If txtMaHopDongLD.Text = "" Then
            MessageBox.Show("Mã hợp đồng không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtMaHopDongLD.Focus()
            Return False
        End If
        If txtTenHopDongLD.Text = "" Then
            MessageBox.Show("Tên hợp đồng không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtTenHopDongLD.Focus()
            Return False
        End If
        Return True
    End Function

    Protected Overrides Function SaveDictionary() As Boolean
        Dim bResult As Boolean = False
        Try
            ptyHopDongLD.MaHopDongLD = txtMaHopDongLD.Text
            ptyHopDongLD.TenHopDongLD = txtTenHopDongLD.Text
            ptyHopDongLD.NgayKy = dtNgayKy.Value
            ptyHopDongLD.SoLoaiHDLD = cboSoLoaiHDLD.Value
            ptyHopDongLD.NgayHieuLuc = dtNgayHieuLuc.Value
            ptyHopDongLD.NgayHetHieuLuc = dtNgayHetHieuLuc.Value
            ptyHopDongLD.SoViTriCV = cboSoViTriCV.Value
            ptyHopDongLD.SoNV = cboSoNV.Value
            ptyHopDongLD.HinhThucLamViec = cboHinhThucLamViec.Value
            ptyHopDongLD.MucLuongBaoHiem = ceMucLuongBaoHiem.Value
            ptyHopDongLD.MucLuongThoaThuan = ceMucLuongThoaThuan.Value
            ptyHopDongLD.PhuCap = txtPhuCap.Text
            ptyHopDongLD.NguoiDaiDienKy = txtNguoiDaiDienKy.Text
            ptyHopDongLD.ChucDanh = txtChucDanh.Text
            ptyHopDongLD.DienGiai = txtDienGiai.Text
            'ptyHopDongLD.TepTinDinhKem = txtTepTinDinhKem.Text

            If (eActionMode = EditMode.AddNew) Then
                oHopDongLD.InsertHopDongLD(sConn, ptyHopDongLD)
            ElseIf (eActionMode = EditMode.Edit) Then
                ptyHopDongLD.SoHopDongLD = RowDetail.Item("SoHopDongLD")
                oHopDongLD.UpdateHopDongLD(sConn, ptyHopDongLD)
            End If

            If (grdList.Rows.Count > 0) Then
                SaveTaiLieuDinhKem()
            End If

            bResult = True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            bResult = False
        End Try
        Return bResult
    End Function

    Private Sub LoadAllCombo()
        Try
            ds.Clear()
            If (Not ds.Tables("LoaiHopDongLD") Is Nothing) Then
                ds.Tables("LoaiHopDongLD").Rows.Clear()
            End If
            SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Get_LoaiHopDongLD_By_HoatDong", ds, New String() {"LoaiHopDongLD"})
            If (Not ds.Tables("LoaiHopDongLD") Is Nothing) Then
                With cboSoLoaiHDLD
                    .DataSource = ds.Tables("LoaiHopDongLD")
                    .DisplayMember = "TenLoaiHDLD"
                    .ValueMember = "SoLoaiHDLD"
                    .DisplayLayout.Load(PathLayout & "cboLoaiHopDongLD.lyt")
                End With
            End If

            If (Not ds.Tables("NhanVien") Is Nothing) Then
                ds.Tables("NhanVien").Rows.Clear()
            End If
            SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_NhanViens", ds, New String() {"NhanVien"})
            If (Not ds.Tables("NhanVien") Is Nothing) Then
                With cboSoNV
                    .DataSource = ds.Tables("NhanVien")
                    .DisplayMember = "TenNV"
                    .ValueMember = "SoNV"
                    .DisplayLayout.Load(PathLayout & "cboNhanVien.lyt")
                End With
            End If

            If (Not ds.Tables("ViTriCongViec") Is Nothing) Then
                ds.Tables("ViTriCongViec").Rows.Clear()
            End If
            SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Get_ViTriCongViec_By_HoatDong", ds, New String() {"ViTriCongViec"})
            If (Not ds.Tables("ViTriCongViec") Is Nothing) Then
                With cboSoViTriCV
                    .DataSource = ds.Tables("ViTriCongViec")
                    .DisplayMember = "TenViTriCV"
                    .ValueMember = "SoViTriCV"
                    .DisplayLayout.Load(PathLayout & "cboViTriCongViec.lyt")
                End With
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Lỗi: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub cboSoLoaiHDLD_ItemNotInList(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.ValidationErrorEventArgs) Handles cboSoLoaiHDLD.ItemNotInList
        cboSoLoaiHDLD.Value = Nothing
    End Sub

    Private Sub cboSoNV_ItemNotInList(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.ValidationErrorEventArgs) Handles cboSoNV.ItemNotInList
        cboSoNV.Value = Nothing
    End Sub

    Private Sub cboSoNV_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSoNV.ValueChanged
        If (cboSoNV.IsItemInList) Then
            txtMaNV.Text = ds.NhanVien.FindBySoNV(cboSoNV.Value).MaNV
        End If
    End Sub

    Private Sub cboSoViTriCV_ItemNotInList(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.ValidationErrorEventArgs) Handles cboSoViTriCV.ItemNotInList
        cboSoViTriCV.Value = Nothing
    End Sub

    Private Sub btnChonTepTin_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonTepTin.Click
        'rowTLDK = tableTLDK.NewRow
        'With rowTLDK
        '    .Item("TenTaiLieuDinhKem") = ""
        '    .Item("TenTepTin") = ""
        '    .Item("DienGiai") = ""
        '    .Item("DinhDangTepTin") = True
        'End With
        'tableTLDK.Rows.Add(rowTLDK)
        Dim ofrm As frmTaiLieuDinhKemDetail = New frmTaiLieuDinhKemDetail()
        ofrm.m_passdata = New frmTaiLieuDinhKemDetail.PassData(AddressOf GetValue)
        If Not IsNothing(ofrm) Then
            With ofrm
                .RowDetail = rowTLDK
                .ActionMode = frmTaiLieuDinhKemDetail.EditMode.AddNew
                .TaiLieuDinhKemTable = tableTLDK
                .ShowDialog()
            End With

        End If
    End Sub

    Private Sub btnXoaTepTin_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaTepTin.Click
        Dim oArray As SqlParameter() = {New SqlParameter("SoTaiLieuDinhKem", SqlDbType.Int)}
        If (MessageBox.Show("Bạn có muốn xóa tệp tin này không?", Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK) Then
            taiLieuDK(index) = grdList.ActiveRow.Cells("SoTaiLieuDinhKem").Value
            grdList.ActiveRow.Delete(False)
            index = index + 1
            For i As Integer = 0 To index - 1
                oArray(0).Value = taiLieuDK(i)
                SqlHelper.ExecuteScalar(sConn, CommandType.StoredProcedure, "Delete_TaiLieuDinhKem", oArray)
            Next
        End If
    End Sub

    Public Sub LoadDataGrid()
        Try
            Dim ds As New DataSet
            Dim oArray As SqlParameter() = {New SqlParameter("@MaHopDong", SqlDbType.VarChar, 20)}
            oArray(0).Value = RowDetail.Item("MaHopDongLD")
            If (TableTaiLieuDinhKem.Rows.Count = 0) Then
                SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Get_TaiLieuDinhKem_By_MaHopDong", ds, New String() {"TaiLieuDinhKem"}, oArray)
                tableTLDK = ds.Tables("TaiLieuDinhKem")
            Else
            End If
            grdList.DataSource = tableTLDK
            With grdList.DisplayLayout.Bands(0)
                .Columns("SoTaiLieuDinhKem").Hidden = True
                .Columns("MaHopDong").Hidden = True
                .Columns("NoiDungTepTin").Hidden = True
                .Columns("TenTepTin").Hidden = True

                .Columns("TenTaiLieuDinhKem").Header.Caption = "Tên tài liệu"
                .Columns("KichThuocTaiLieuDinhKem").Header.Caption = "Kích thước (KB)"
                .Columns("DinhDangTepTin").Header.Caption = "Định dạng tệp tin"
                .Columns("DienGiai").Header.Caption = "Diễn giải"
            End With
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
       
    End Sub

    Private Sub SaveTaiLieuDinhKem()
        Dim ptyTaiLieuDK As New TaiLieuDinhKem
        Dim oTaiLieuDK As New BusTaiLieuDinhKem
        Dim ds_1 As New DataSet
        Dim oArray As SqlParameter() = {New SqlParameter("@SoTaiLieuDinhKem", SqlDbType.Int)}
        Try
            Dim dr As Infragistics.Win.UltraWinGrid.UltraGridRow
            For Each dr In grdList.Rows
                oArray(0).Value = dr.Cells("SoTaiLieuDinhKem").Value
                SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Get_TaiLieuDinhKem_By_SoTaiLieuDinhKem", ds_1, New String() {"TaiLieuDinhKem"}, oArray)
                ptyTaiLieuDK.MaHopDong = txtMaHopDongLD.Text
                ptyTaiLieuDK.TenTaiLieuDinhKem = dr.Cells("TenTaiLieuDinhKem").Value
                ptyTaiLieuDK.TenTepTin = dr.Cells("TenTepTin").Value
                ptyTaiLieuDK.KichThuocTaiLieuDinhKem = dr.Cells("KichThuocTaiLieuDinhKem").Value
                ptyTaiLieuDK.DinhDangTepTin = dr.Cells("DinhDangTepTin").Value
                ptyTaiLieuDK.DienGiai = dr.Cells("DienGiai").Value
                ptyTaiLieuDK.NoiDungTepTin = dr.Cells("NoiDungTepTin").Value

                If (ds_1.Tables("TaiLieuDinhKem").Rows.Count > 0) Then
                    ptyTaiLieuDK.SoTaiLieuDinhKem = dr.Cells("SoTaiLieuDinhKem").Value
                    oTaiLieuDK.UpdateTaiLieuDinhKem(sConn, ptyTaiLieuDK)
                Else
                    oTaiLieuDK.InsertTaiLieuDinhKem(sConn, ptyTaiLieuDK)
                End If

            Next
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

End Class
